<?php
session_start();
include_once ("page.php");
include_once ("videoPlayer.php");
include_once ("video.php");
include_once ("annotationList.php");
include_once ("mysqllib.php");
include_once ("loginPage.php");
include_once ("videoList.php");
include_once ("globals.php");
include_once ("uploadVideo.php");
include_once ("user.php");

//Handle everything needed for any page

$page = new page();
$db = new mysqllib();

$currentPage = "login";

//get the page requested or display the login page if no request
if (isset($_GET['page'])) {
	$currentPage = htmlentities($_GET['page']);
	//if a page is request check if the user is logged in
	if (!isset($_SESSION['idUser'])) {
		$currentPage = "login";
		$_GET['action'] = "forbid";
	}

}

switch($currentPage) {
	case "login" :
		$action = "";
		if (isset($_GET['action'])) {
			$action = htmlentities($_GET['action']);
		}
		$page -> printHeaderHTMLLogin();
		$loginPage = new loginPage();
		$loginPage -> displayLoginForm($action);
		break;
	case "signup" :
		$page -> printHeaderHTMLLogin();
		$signUpPage = new loginPage();
		//check if the sign in has failed before
		$action = "";
		if (isset($_GET['action'])) {
			$action = htmlentities($_GET['action']);
		}
		$signUpPage -> displaySignupForm($action);

		break;
	case "video" :

		//check if the user is login and allowed to access the video
		if (!isset($_SESSION['idUser'])) {
			header("location:index.php?page=login&action=forbid");
		} else {
			$db = new mysqllib();
			$idUser = $_SESSION['idUser'];
			$idVideo = $_GET['id'];
			//Check if the user is registered in the course the video is listed under
			$query = "SELECT * FROM IsRegistered JOIN Video ON IsRegistered.idCourse = Video.idCourse WHERE idUser = $idUser AND idVideo = $idVideo";
			$result = mysql_query($query);
			if (mysql_num_rows($result) == 1) {
				//if allowed, display the video page
				$page -> printHeaderHTMLVideoPage();

				$video = new video();

				$video -> getVideoById($idVideo);

				$videoPlayer = new videoPlayer($video);
				$videoPlayer -> displayAll();
			} else {
				//not allowed to see the video
				header("location:index.php?page=videoList&action=forbid");
			}
		}
		break;
	case "videoList" :
		$db = new mysqllib();
		$page -> printHeaderHTMLVideoList();
		$videoList = new videoList();
		//check if any action is set
		if (isset($_GET['action'])) {
			switch($_GET['action']) {
				case 'forbid' :
					echo '<h3 id="errorAccess"> You are not registered for the class of this video. Here are the videos you can access.</h3>';
					break;
				case 'notadmin' :
					echo '<h3 id="errorAccess"> You have to be an administrator to access this page.</h3>';
					break;
				case 'uSucess' :
					echo '<h3> The video and its slides were successfully added.</h3>';
					break;
			}
		}
		$videoList -> displayAllVideosForUser();
		break;
	case "adminPage" :
		if ($_SESSION['status'] == $GLOBALS['admin']) {
			$page -> printHeaderHTMLVideoList();
			echo '<h3>Admin Page</h3>';
			echo '<button onclick="location.href=\'index.php?page=upload\'">Add a video</button><br/>';
			echo '<button onclick="location.href=\'index.php?page=block\'">Block a user</button><br/>';
		} else {
			header("location:index.php?page='videoList'&action='notadmin'");
			exit ;
		}
		break;
	case "upload" :
		$db = new mysqllib();
		if ($_SESSION['status'] == $GLOBALS['admin']) {
			$page -> printHeaderHTMLUpload();
			$uploadVideo = new uploadVideo();
			$uploadVideo -> displayUploadForm();
			parse_str($_SERVER['QUERY_STRING'], $param);
			$uploadVideo -> errorHandling($param['errV'], $param['errS']);
		} else {
			header("location:index.php?page=videoList&action=notadmin");
			exit ;
		}
		break;
	case "block" :
		if ($_SESSION['status'] == $GLOBALS['admin']) {
			$user = new user();
			$page -> printHeaderHTMLBlock();
			$user -> displayAllUsers();
		} else {
			header("location:index.php?page='videoList'&action='notadmin'");
			exit ;
		}
		break;
}

$page -> printFootHTML();
?>
